package org.budo.cmbc.direct.sdk;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.budo.cmbc.direct.sdk.entity.MEAccountAssetsQueryRequest;
import org.budo.cmbc.direct.sdk.entity.MEAccountAssetsQueryResponse;
import org.budo.cmbc.direct.sdk.entity.MEAccountOpenQueryRequest;
import org.budo.cmbc.direct.sdk.entity.MEAccountOpenQueryResponse;
import org.budo.cmbc.direct.sdk.entity.MEAccountOpenRequest;
import org.budo.cmbc.direct.sdk.entity.MEAccountQueryRequest;
import org.budo.cmbc.direct.sdk.entity.MEAccountQueryResponse;
import org.budo.cmbc.direct.sdk.entity.MEAddrListQryRequest;
import org.budo.cmbc.direct.sdk.entity.MEAddrListQryResponse;
import org.budo.cmbc.direct.sdk.entity.MEBankInnerPathQueryRequest;
import org.budo.cmbc.direct.sdk.entity.MEBankInnerPathQueryResponse;
import org.budo.cmbc.direct.sdk.entity.MEEAcnoTrsJnlQueryRequest;
import org.budo.cmbc.direct.sdk.entity.MEEAcnoTrsJnlQueryResponse;
import org.budo.cmbc.direct.sdk.entity.MELEBankRouterOutTransferRequest;
import org.budo.cmbc.direct.sdk.entity.MELEBankRouterOutTransferResponse;
import org.budo.cmbc.direct.sdk.entity.MELEBankRouterQueryRequest;
import org.budo.cmbc.direct.sdk.entity.MELEBankRouterQueryResponse;
import org.budo.cmbc.direct.sdk.entity.MELEGenerateRandRequest;
import org.budo.cmbc.direct.sdk.entity.MELEGetPhoneCodeRequest;
import org.budo.cmbc.direct.sdk.entity.MELEIdPhotoUploadRequest;
import org.budo.cmbc.direct.sdk.entity.MELEIdPhotoUploadResultRequest;
import org.budo.cmbc.direct.sdk.entity.MEOccupationListQryRequest;
import org.budo.cmbc.direct.sdk.entity.MEOccupationListQryResponse;
import org.budo.cmbc.direct.sdk.entity.MEQryTransStatusRequest;
import org.budo.cmbc.direct.sdk.entity.MEQryTransStatusResponse;
import org.budo.cmbc.direct.sdk.entity.changeaccountinfo.MEApprovalProcessListQueryRequest;
import org.budo.cmbc.direct.sdk.entity.changeaccountinfo.MEApprovalProcessListQueryResponse;
import org.budo.cmbc.direct.sdk.entity.changeaccountinfo.MEApprovalProcessQueryRequest;
import org.budo.cmbc.direct.sdk.entity.changeaccountinfo.MEApprovalProcessQueryResponse;
import org.budo.cmbc.direct.sdk.impl.CmbcDirectBankSdkImpl;
import org.budo.support.lang.util.IoUtil;
import org.junit.Test;
import org.springframework.util.Base64Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lmw
 */
public class CmbcDirectBankSdkTest_budo {
    /**
     * 1 上传身份证
     */
    @Test
    public void MELEIdPhotoUpload() {
        MELEIdPhotoUploadRequest request = new MELEIdPhotoUploadRequest();
        request.setCifName("了解活动");
        request.setIdNo("450354198501153546");
        request.setIdPASide(Base64Utils.encodeToString(IoUtil.toByteArray(new File("C:/Users/Administrator/Pictures/IMG_idpa.jpg"))));
        request.setIdPBSide(Base64Utils.encodeToString(IoUtil.toByteArray(new File("C:/Users/Administrator/Pictures/IMG_idpb.jpg"))));

        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        cmbcDirectBankSdk.MELEIdPhotoUpload(request);

        // {"message":"success","Status":"00","ChannelJnlNo":"62169220191120228679691995430000","code":"DS00000","IdPNo":"31301201911201638806006313000000","Reason":null}
    }

    /**
     * 2 生成随机数
     */
    @Test
    public void MELEGenerateRand() {
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();

        cmbcDirectBankSdk.MELEGenerateRand(new MELEGenerateRandRequest());
        // {"message":"success","RandJnlNo":"31301201911201638810176313000000","ChannelJnlNo":"62169220191120228734609995430000","code":"DS00000","Random":"VdAx3OoFAVogxyFc"}
    }

    /**
     * 3 发送短信验证码
     */
    @Test
    public void MELEGetPhoneCode() {
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();

        MELEGetPhoneCodeRequest request = new MELEGetPhoneCodeRequest() //
                .setMobilePhone("17333249028");
        cmbcDirectBankSdk.MELEGetPhoneCode(request);
        // {"message":"success","MessageTaskId":"b1c0a9e291044919b2a66e9244c4c17b","ChannelJnlNo":"62169220191120228763189995430000","code":"DS00000","MobilePhone":"17333249028","ExpiredTime":"120"}
    }

    /**
     * 4 开户(客户端密码控件)
     */
    @Test
    public void MEAccountOpen() {
        MEAccountOpenRequest request = new MEAccountOpenRequest();

        request.setIdPNo("31301201911201638806006313000000");

        request.setIdNo("063366199003083661");
        request.setCifName("测试一");
        request.setMobilePhone("17333249028");
        request.setTAcNo("4129638427386421");

        request.setRandJnlNo("31301201911201638810176313000000");
        request.setRandom("VdAx3OoFAVogxyFc");

        request.setMessageTaskId("b1c0a9e291044919b2a66e9244c4c17b");
        request.setMessageCode("210074");

        request.setPwdResult("01031421@ELdKQS69xO6AEwR5XnD0b+KFtgV1lEqA0DeAdSl3gUoxJA73C6JfBE3qu/xeaUQv6eh51lwHe9OaWjrNX5yltzyfH0KCm4CX1X0TQpSShqoYWk55lkUjEmXx2ag/K+HxJ0A5/xM4a8vcyTKss65k/cSBTTxuFErqDSoDTH7O/IM=");
        request.setPwdResultConfirm("01031421@PB8jPV7BqOaWraDZFSLa0NtI/4xvaXuEnjdoq9EgQNXim3ANrNGBlA6mT8Sv+Jaxq8KXOBcI7ob451PARGHMquvAYEIsceiL982x5aeCZxTx76EC+a+oX7QbhuirF6saMoWSss/IiGpTW+xo+tdoEEfO+BWhdbVYFijvXowuZEg=");

        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        cmbcDirectBankSdk.MEAccountOpen(request);
        // {"BankInnerFlag":null,"CardSignFlag":null,"code":"DS00000","EAccountOpenJnlNo":"31301201911201638817286313000000","ShadowEAcNo":null,"message":"success","FundAcNo":null,"ShadowEProtocolAcNo":null,"OpenDate":null,"ChannelJnlNo":"62169220191120228861714995430000","EAcNoStatus":"02","EAcNo":null,"EProtocolAcNo":null}
    }

    /**
     * 5 查询开户结果
     */
    @Test
    public void MEAccountOpenQuery() {
        MEAccountOpenQueryRequest request = new MEAccountOpenQueryRequest();
        String[] nos = { "313F4202001163000001850313100000" };
        List<String> realNos = new ArrayList<>();
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        for (String no : nos) {
            request.setOriginateChannelSerialNo(no);
            request.setChannelJnlNo("313F4202003203100000002313100000");
            System.out.println(no + ", 开户状态查询");
            MEAccountOpenQueryResponse response = cmbcDirectBankSdk.MEAccountOpenQuery(request);
            System.out.println(no + ", 开户状态结果=" + JSON.toJSONString(response));
            if (response.getQueryStatus().equals("02")) {
                realNos.add(no);
            }
        }
//        System.out.println(String.join(",", realNos));

//        System.out.println(JSON.toJSONString(response));
    }

    @Test
    public void MEBankInnerPathQuery() {
        MEBankInnerPathQueryRequest request = new MEBankInnerPathQueryRequest();
        request.setEProtocolAcNo("201030025008738635").setSendMsgFlag("N");
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        request.setChannelJnlNo("313F4202003313200000001313100000");
        MEBankInnerPathQueryResponse response = cmbcDirectBankSdk.MEBankInnerPathQuery(request);
        System.out.println(JSON.toJSONString(response));
    }

    @Test
    public void MEApprovalProcessListQuery() {
        MEApprovalProcessListQueryRequest request = new MEApprovalProcessListQueryRequest();
        request.setEProtocolAcNo("201040001008805875");
        request.setChannelJnlNo("313F4202004293900000002313100000");
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        MEApprovalProcessListQueryResponse response = cmbcDirectBankSdk.MEApprovalProcessListQuery(request);
        System.out.println("-------------------" + JSON.toJSONString(response));
    }

    @Test
    public void MEApprovalProcessQuery() {
        MEApprovalProcessQueryRequest request = new MEApprovalProcessQueryRequest();
        request.setEProtocolAcNo("201020023008267459");
        request.setOriginateChannelSerialNo("313F4202006123000000800313100000");
        request.setChannelJnlNo("313F4202006153900000001313100000");
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        MEApprovalProcessQueryResponse response = cmbcDirectBankSdk.MEApprovalProcessQuery(request);
        System.out.println(JSON.toJSONString(response));
    }

    @Test
    public void MELEIdPhotoUploadResult() {
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        MELEIdPhotoUploadResultRequest request = new MELEIdPhotoUploadResultRequest();
        request.setIdNo("13240119791202316X");
        request.setChannelJnlNo("313F4202001133200000031313100000");
        cmbcDirectBankSdk.MELEIdPhotoUploadResult(request);
    }

    @Test
    public void MECustInfoCheck() {
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();

        cmbcDirectBankSdk.MECustInfoCheck(null);
    }

    @Test
    public void MELECardBinQuery() {
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();

        cmbcDirectBankSdk.MELECardBinQuery(null);
    }

    /**
     * 查询出金汇路接口
     */
    @Test
    public void bankRouterQuery() {
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        MELEBankRouterQueryRequest request = new MELEBankRouterQueryRequest().setAmount("100.2").setEProtocolAcNo("191120001000052669");

        MELEBankRouterQueryResponse response = cmbcDirectBankSdk.MELEBankRouterQuery(request);
        System.out.println(JSON.toJSONString(response));
    }

    /**
     * 入金汇路查询
     */
    @Test
    public void bankInnerPathQuery() {
        System.out.println("入金汇路查询");
        CmbcDirectBankSdk cmbcDirectBankSdk = this.cmbcDirectBankSdk();
        MEBankInnerPathQueryRequest request = new MEBankInnerPathQueryRequest().setEProtocolAcNo("201120004011427074");
        request.setChannelJnlNo(staticJnlNo);
        MEBankInnerPathQueryResponse response = cmbcDirectBankSdk.MEBankInnerPathQuery(request);
        System.out.println(JSON.toJSONString(response));
    }

    /**
     * 电子账户资产查询接口(电子账户余额)
     */
    @Test
    public void bankAssetQuery() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
//        MEAccountAssetsQueryRequest request = new MEAccountAssetsQueryRequest().setEProtocolAcNo("201030017008629447");
        MEAccountAssetsQueryRequest request = new MEAccountAssetsQueryRequest().setEProtocolAcNo("211030025012651176");
        request.setChannelJnlNo(staticJnlNo);
        MEAccountAssetsQueryResponse response = sdk.MEAccountAssetsQuery(request);
        System.out.println("==========>" + JSON.toJSONString(response));
    }

    /**
     * 电子账户资产查询接口(电子账户余额)
     */
    @Test
    public void bankAssetQueryBatch() {
        String allNos = "191120030007569511,191120030007571739";
        String[] allNoList = allNos.split(",");
//        313F4202012073400000004313100000
        String oriPrefix = "313F4202012073400000000";
        int startIndex = 1000;
        int index = startIndex;
        String oriSuffix = "313100000";
        List<String> closetNos = Lists.newArrayList();
        List<String> notClosetNos = Lists.newArrayList();
        for (int i = 0; i < allNoList.length; i++) {
            index += 1;
            String eNo = allNoList[i];
            System.out.println("执行第" + i + "条数据, No=" + eNo);
            CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
            MEAccountAssetsQueryRequest request = new MEAccountAssetsQueryRequest().setEProtocolAcNo(eNo);
            String indexStr = Integer.toString(index);
            String prefix = oriPrefix.substring(0, oriPrefix.length() - indexStr.length());
            String JnlNo = prefix + indexStr + oriSuffix;
            request.setChannelJnlNo(JnlNo);
            MEAccountAssetsQueryResponse response = sdk.MEAccountAssetsQuery(request);
            if ("DS00000".equals(response.getCode())) {
                notClosetNos.add(eNo);
            } else {
                closetNos.add(eNo);
            }
        }
        System.out.println("close ====>" + JSON.toJSONString(closetNos));
        System.out.println("notClose ====>" + JSON.toJSONString(notClosetNos));
    }

    /**
     * 查询交易记录
     */
    @Test
    public void accountTrsJnlQuery() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
        MEEAcnoTrsJnlQueryRequest request = new MEEAcnoTrsJnlQueryRequest().setEProtocolAcNo("201020005007907115").setBeginDate("20210101").setEndDate("20210409");
        request.setChannelJnlNo("313F4202104093900000002313100000");
        MEEAcnoTrsJnlQueryResponse response = sdk.MEEAcnoTrsJnlQuery(request);
        System.out.println("==========>" + JSON.toJSONString(response));
    }

    /**
     * 电子账户信息查询
     */
    @Test
    public void bankAccountQuery() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
        MEAccountQueryRequest request = new MEAccountQueryRequest().setEProtocolAcNo("201040018009041525");
        request.setChannelJnlNo(staticJnlNo);
        MEAccountQueryResponse response = sdk.MEAccountQuery(request);
        System.out.println("bankAccountQuery:" + JSON.toJSONString(response));
    }

    /**
     * 职业列表查询
     */
    @Test
    public void MEOccupationListQry() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
        MEOccupationListQryRequest request = new MEOccupationListQryRequest();
        request.setChannelJnlNo(taxJnlNo);
        MEOccupationListQryResponse response = sdk.MEOccupationListQry(request);
        System.out.println("---->" + JSON.toJSONString(response));
    }

    /**
     * 出金接口
     */
    @Test
    public void routerOutTransfer() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
        MELEBankRouterOutTransferRequest request = new MELEBankRouterOutTransferRequest().setPayPath("0").setEProtocolAcNo("191120002000052728").setAmount("0.5").setRandJnlNo("31301201912031690000666313000000").setRandom("RMeXltkdBH5A8CWZ").setPwdResult("01031421@SwMiVW7YGSsxKm8aAGLuUWoljB7lYvA6HKBtQtaQij1Kw5XahVRwusk50QAzGm0MS2Pxj2NYxhB9RBe7J8+j0m32PE6O30nJ+3p+Dc6/KIJPjBqE5eh44G1Kc/IYw6sRto0h58tBZeqlgkC1SIyKvoyvvBRsovtiwkZbbsA/uTY=");
        MELEBankRouterOutTransferResponse response = sdk.MELEBankRouterOutTransfer(request);
        System.out.println("----" + JSON.toJSONString(response));
    }

    @Test
    public void MEAddrListQry() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
        MEAddrListQryResponse response = sdk.MEAddrListQry(new MEAddrListQryRequest());
        System.out.println("----" + JSON.toJSONString(response));
    }

    /**
     * 不确定交易查询
     */
    @Test
    public void queryTransStatus() {
        CmbcDirectBankSdk sdk = this.cmbcDirectBankSdk();
        MEQryTransStatusRequest request = new MEQryTransStatusRequest().setEProtocolAcNo("191120002000052728").setOriginateChannelSerialNo("62169220191203349822979995430000").setOriginateChannelDate("20191203");
        MEQryTransStatusResponse response = sdk.MEQryTransStatus(request);
        System.out.println("----" + JSON.toJSONString(response));
    }



    String staticJnlNo = "313F4202104193900000001313100000";
    String taxJnlNo = "313G3202104087900000002313100000";

    private CmbcDirectBankSdk cmbcDirectBankSdk() {
        CmbcDirectBankSdkImpl cmbcDirectBankSdk = new CmbcDirectBankSdkImpl();

        cmbcDirectBankSdk.setMerchantNo("99543");
//        https-bbj
//        cmbcDirectBankSdk.setUrl("https://111.205.207.136:41414/webgateway/dtconnect.do");
//        cmbcDirectBankSdk.setUserPrivateFilePassword("123cmbc");
//        https-uat
//        cmbcDirectBankSdk.setUrl("https://111.205.207.136:55016/webgateway/dtconnect.do");
//        cmbcDirectBankSdk.setUserPrivateFilePassword("111111");
//        cmbcDirectBankSdk.setJksFilePassword("111111");
//        String file = this.getClass().getResource("/313Banknew.cer").getFile();
//        String file2 = this.getClass().getResource("/shanghunew.sm2").getFile();
//        String file3 = this.getClass().getResource("/client2019.jks").getFile();
//        String file4 = this.getClass().getResource("/trust2019.jks").getFile();

        // 淘金配置
        cmbcDirectBankSdk.setUrl("https://pay.mszxyh.com/webgateway/dtconnect.do");
        cmbcDirectBankSdk.setUserPrivateFilePassword("Itaojin*2018");
        cmbcDirectBankSdk.setJksFilePassword("Itaojin*2018");
        String file = this.getClass().getResource("/cmbc.cer").getFile();
        String file2 = this.getClass().getResource("/1268342875.sm2").getFile();
        String file3 = this.getClass().getResource("/cmbc.jks").getFile();
        String file4 = this.getClass().getResource("/cmbc.jks").getFile();

        // 筹控配置
//        cmbcDirectBankSdk.setMerchantNo("99637");
//        cmbcDirectBankSdk.setUrl("https://pay.mszxyh.com/webgateway/dtconnect.do");
//        cmbcDirectBankSdk.setUserPrivateFilePassword("ChouKong*2021");
//        cmbcDirectBankSdk.setJksFilePassword("ChouKong*2021");
//        String file = this.getClass().getResource("/cmbc.cer").getFile();
//        String file2 = this.getClass().getResource("/choukong-sm.sm2").getFile();
//        String file3 = this.getClass().getResource("/choukong-cmbc-0407.jks").getFile();
//        String file4 = this.getClass().getResource("/choukong-cmbc-0407.jks").getFile();

        System.err.println("file=" + file);
        System.err.println("file2=" + file2);

        cmbcDirectBankSdk.setPublicKeyPath(file);
        cmbcDirectBankSdk.setPrivateKeyPath(file2);
        cmbcDirectBankSdk.setClientJksPath(file3);
        cmbcDirectBankSdk.setTrustJksPath(file4);
        return cmbcDirectBankSdk;
    }
}
